#pragma once
#define NOMINMAX
#include "../tfbondlib.h"
#include <QObject>
#include <vector>
#include <map>
#include <mutex>
#include <set>
#include <atomic>
#include <qbprotocol/include/SSQBModel.h>
#include <qbtools/message/MessagePoster.h>

class TfKLineTypeData_1M
{
public:
	TfKLineTypeData_1M();
	//更新数据
	void update(const xQBTFPriceList_c& DataList, int& BeginIndex, int& EndIndex);
	//更新数据
	void response(const std::vector<xQBTFRealTimeUnit_c>& DataList, int& BeginIndex, int& EndIndex);
	//获取数据
	bool getRealtime(std::vector<xQBTFRealTimeUnit_c>& Item, int BeginIndex, int EndIndex);
	//获取快照
	bool getSnapshot(xQBTFRealTimeUnit_c& Snapshot);
	//获取更新区间
	void getRange(int& BeginIndex, int& EndIndex);
	//产生k 线
	void makeKLine(time_t t, int& BeginIndex, int& EndIndex);
	//清理数据
	void clear();

private:
	//时间对应的Index
	static int time2Index(int hour, int minute, int second);
	static int time2Index(time_t t);
	//Index对应的时间
	time_t index2Time(int index) const;
	//填充数据
	void fill(int index);

private:
	std::vector<xQBTFRealTimeUnit_c>	m_list;
	bool	m_enableMakeKline = false;
	std::mutex	m_mutex;
};

class TfKLineRealTime_1M;
class TfKLineTypeList_1M
{
	enum ReqStatus
	{
		kNone,
		KSent,
		KReceived
	};
public:
	TfKLineTypeList_1M(const char* pId, const char* pKey, TfKLineRealTime_1M*);

	//更新数据
	void update(const xQBTFPriceList_c& DataList);
	//更新数据
	void response(const std::vector<xQBTFRealTimeUnit_c>& DataList, bool finished);
	//获取数据
	bool getRealtime(std::vector<xQBTFRealTimeUnit_c>& Item, int BeginIndex, int EndIndex);
	//获取快照
	bool getSnapshot(xQBTFRealTimeUnit_c& Snapshot);

	void request();

	//产生k 线
	void makeKLine(time_t t);

	//清盘信号
	void tfOpen();

	void registerPush();
	void unRegisterPush();
	bool hasReg();

private:
	std::mutex m_mutexRegister;
	TfKLineTypeData_1M m_data;
	volatile ReqStatus m_reqstatus = kNone;
	std::string m_id;
	std::string m_key;
	TfKLineRealTime_1M* m_pMsgReceiver = nullptr;
	int	m_registers = 0;
	bool m_sendPush = false;
};

class S_TFBONDLIB_EXPORT TfKLineRealTime_1M : public QObject, public MessageReceiver
{
	Q_OBJECT
public:
	static TfKLineRealTime_1M& instance();

	//获取当日数据
	void getKLine1M(const char* pId);
	//获取实时数据
	bool getRealtime(const char* pId, std::vector<xQBTFRealTimeUnit_c>& Item, int BeginIndex = -1, int EndIndex = -1);
	//获取快照
	bool getSnapshot(const char* pId, xQBTFRealTimeUnit_c& Snapshot);

	void registerPush(const std::string& id);
	void unRegisterPush(const std::string& id);
	//清理
	void Clear();
public:
	void onDataArrived(const qb::SSAckMsg& msg) override;

signals:
	void kLineReqDataArrived(QString id, int beginIndex, int endIndex);
	void kLinePushDataArrived(QString id, int beginIndex, int endIndex);

protected:
	void timerEvent(QTimerEvent* event) override;
	//由TF id获取对应的数据
	std::shared_ptr<TfKLineTypeList_1M> GetTfData(const char* pId, bool Add = true);

private:
	TfKLineRealTime_1M() = default;
	void makeKLine();

	std::mutex m_mutex;
	time_t m_LastTime;	//上一次时间
	std::map <std::string, std::shared_ptr<TfKLineTypeList_1M> > m_List;
	int m_timerId = -1;
};